﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Text;
using T3K.SketchEditor.Core;
using T3K.SketchEditor.Forms;
using T3K.SketchEditor.Configuration;
using System.Xml.Serialization;


namespace T3K.SketchEditor.Plugins {

    public abstract class PluginBase : IDisposable {

        private string _pluginName;
        private SettingsBase _settings;
        private T3K.SketchEditor.Core.EditorManager _editor = T3K.SketchEditor.Core.EditorManager.Instance;

        public event EditorEventHandler EditorEvent;

        public T3K.SketchEditor.Core.EditorManager Editor {
            get { return _editor; }
            set { _editor = value; }
        }

        public string PluginName {
            get { return _pluginName; }
            set { _pluginName = value; }
        }

        public SettingsBase Settings {
            get { return _settings; }
            set { _settings = value; }
        }

        public PluginBase(string pluginName) {
            this._pluginName = pluginName;
            _editor.RegisterCommands(this, pluginName);
        }

        #region EditorEvent
        public void EditorEventHandler(object sender, EditorEventArgs args) {
            OnEditorEvent(sender, args);
        }

        protected void OnEditorEvent(object sender, EditorEventArgs args) {
            if (EditorEvent != null)
                EditorEvent(sender, args);
        }
        #endregion

        #region Dispose & Finalise
        public void Dispose() {
            Clean();
            GC.SuppressFinalize(this);
        }

        private void Clean() {
            _editor.UnregisterCommands(this, _pluginName);
        }

        ~PluginBase() {
            Clean();
        }
        #endregion

        #region Editor Plugin API

        #region MainMenu API
        public bool MainMenu_InsertItemAfter(ToolStripMenuItem item, string itemLocation) {

            CommandArgs args = new CommandArgs();
            args["Item"] = itemLocation;
            Editor.InvokeCommand(this, "Main.GetMenuItem", args);

            ToolStripMenuItem cMenuItem = args.Return.Data as ToolStripMenuItem;

            if (cMenuItem != null) {
                if (cMenuItem.OwnerItem != null) {
                    int index = ((ToolStripMenuItem)cMenuItem.OwnerItem).DropDownItems.IndexOf(cMenuItem);
                    ((ToolStripMenuItem)cMenuItem.OwnerItem).DropDownItems.Insert(index + 1, item);
                    return true;
                } else {
                    int index = cMenuItem.Owner.Items.IndexOf(cMenuItem);
                    cMenuItem.Owner.Items.Insert(index + 1, item);
                    return true;
                }
            } else
                return false;
        }

        public bool MainMenu_InsertItemBefore(ToolStripMenuItem item, string itemLocation) {

            CommandArgs args = new CommandArgs();
            args["Item"] = itemLocation;
            Editor.InvokeCommand(this, "Main.GetMenuItem", args);

            ToolStripMenuItem cMenuItem = args.Return.Data as ToolStripMenuItem;

            if (cMenuItem != null) {
                if (cMenuItem.OwnerItem != null) {
                    int index = ((ToolStripMenuItem)cMenuItem.OwnerItem).DropDownItems.IndexOf(cMenuItem);
                    ((ToolStripMenuItem)cMenuItem.OwnerItem).DropDownItems.Insert(index, item);
                    return true;
                } else {
                    int index = cMenuItem.Owner.Items.IndexOf(cMenuItem);
                    cMenuItem.Owner.Items.Insert(index, item);
                    return true;
                }
            } else
                return false;
        }

        public bool MainMenu_InsertItem(ToolStripMenuItem item, string itemLocation) {

            CommandArgs args = new CommandArgs();
            args["Item"] = itemLocation;
            Editor.InvokeCommand(this, "Main.GetMenuItem", args);

            ToolStripMenuItem cMenuItem = args.Return.Data as ToolStripMenuItem;

            if (cMenuItem != null) {
                cMenuItem.DropDownItems.Add(item);
                return true;
            } else
                return false;
        }
        #endregion

        #region MainForm API

        public void MainForm_RegisterForm(string Name, Type formType, DockState state, bool show, params object[] parameters) {

            CommandArgs args = new CommandArgs();
            args["Name"] = Name;
            args["Type"] = formType;
            args["State"] = state;
            args["Show"] = show;
            args["Parameters"] = parameters;

            Editor.InvokeCommand(this, "Main.RegisterWindow", args);
        }

        public void MainForm_Show(string Name) {

            CommandArgs args = new CommandArgs();
            args["Name"] = Name;

            Editor.InvokeCommand(this, "Main.Show", args);
        }

        //public bool MainForm_Show(BaseForm form, DockState state) {
        //    Editor.InvokeCommand(this, "Main.Show", new CommandArgs(form, state));
        //    return true;
        //}

        //public bool MainForm_Show(BaseForm form) {
        //    Editor.InvokeCommand(this, "Main.Show", new CommandArgs(form));
        //    return true;
        //}

        #endregion


        #endregion

        public abstract bool Init();

    }
}
